package com.jxb.e20241230;

import java.util.Stack;

/**
 * 类注释
 *
 * @author jiaobo
 * @date Created in 2024/12/31 20:55
 **/
public class BasicCalculator1_224 {

    public static void main(String[] args) {

    }

    public static int calcu(String s) {
        //定义栈
        Stack<Integer> stack = new Stack<>();
        //当前的结果
        int result = 0;
        //当前的数字
        int num = 0;
        //字符标识(1代表+，-1代表-)
        int sign = 1;
        //循环遍历
        for (int i = 0;i<s.length();i++) {
            char c = s.charAt(i);
            if (c == ' ') {
               continue;
            }
            if (c >= '0' && c<='9') {
                num = num * 10 + c - '0';
                if (i<s.length() - 1 && s.charAt(i + 1) >= '0' && s.charAt(i + 1) <= '9') {
                    continue;
                }
            }else if (c == '(') {
                //将之前的数据存入栈中
                stack.push(result);
                stack.push(sign);
                result = 0;
                sign = 1;
            }else if (c == '+') {
                num = 0;
                sign = 1;
            }else if (c == '-') {
                num = 0;
                sign = -1;
            }else if (c == ')') {
                sign = stack.pop();
                num = result;
                result = stack.pop();
            }
            result = result + num*sign;
        }
        return result;
    }

}
